package com.wawayaya.db.entitydao;


import com.wawayaya.bean.BookShelf;
import com.wawayaya.bean.Books;
import com.wawayaya.db.DBManager;
import com.wawayaya.db.basedao.BookShelfDao;

import java.util.ArrayList;
import java.util.List;


public class BookShelfEntityDao extends BaseEntityDao<BookShelfDao, BookShelf, Long> {

	@Override
	protected BookShelfDao initEntityDao() {
		return DBManager.newSession().getBookShelfDao();
	}

	public void deleteEntityByKey(Long key) {
		getEntityDao().deleteByKey(key);
	}

	@Override
	public void replaceEntity(BookShelf entity) {
		getEntityDao().insertOrReplace(entity);
	}

	/**
	 *
	 *
	 * @param
	 *//*
	public synchronized List<BookRack> selectShelf(Context  context) {
		List<BookRack>  bookShelfList = null;
		String  uid = utils.getcustomerIdviadevice(context);
		Log.e(" VVV","当前用户:"+uid);

		if("0".equals(uid) || "".equals(uid)){
			return bookShelfList;
		}
		final String  bookCoverUrl =  SharedPreferenceUtils.getConfigUrlByKey(ApiConstans.CONFIG_BOOK_COVER, context);

		String  bsIds =  getUserBsid(uid);
		if("".equals(bsIds)){
			return bookShelfList;
		}
		//1.查询我的书架
		final  List<Books>  myShelfBooks = selectMyShelfBook(uid, bookCoverUrl, context, " limit 0,8 ");

		try {
			  bookShelfList = getEntityDao().queryRaw(" where BS_ID in(" + bsIds + ") order  by  BS_SORT asc  ");

			if(null!=bookShelfList && bookShelfList.size()>0){
				for(int i=0;i<bookShelfList.size();i++){
					final BookRack shelfs =bookShelfList.get(i);

                      shelfs.setIconUrl(shelfs.getIconUrl());
						if(!"".equals(shelfs.getBsSaleFlagStr())){
							shelfs.setBsSaleFlag(new JSONArray(shelfs.getBsSaleFlagStr()));
						}
					  if(shelfs.getBsId() ==1 ){
						  shelfs.setBookList(myShelfBooks);
					  }else{
						  final List<Books> bookList = new ArrayList<>();

						  final  JSONArray  array=new JSONArray(shelfs.getBsSaleFlagStr());

						  for(int f=0;f<array.length();f++){
							  if(f==8){
								  break;
							  }
							  final  JSONObject object=(JSONObject)array.get(f);
							  final  Books  books = selectBookById(object.optInt("bookId"),bookCoverUrl,context);
							  books.setSaleFlag(object.optInt("type"));

							  if(null!=books){
								  bookList.add(books);
							  }
						  }
						  shelfs.setBookList(bookList);
					  }
					bookShelfList.set(i,shelfs);
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return bookShelfList;
	}


	private  String  getUserBsid(String uid){
		String  bsId = "";
		UserEntityDao userDao=new UserEntityDao();
		List<User>  users = userDao.queryEntities("");
		if(null!=users && users.size()>0){
			for(int i=0;i<users.size();i++){
				String   msg = users.get(i).toString();
				TLog.log("out message:" + msg);
			}
		}
		 User user = userDao.queryEntityByWhere("where UID = ?", uid);
		if(null!=user){
			bsId =  user.getBsIds();
		}
		return bsId;
	}

	*//**


	*//**

	 * 封装本的 我的书架   内容,bookShelf  id:0   limit 0,8
	 * @param
	 *//*
	public synchronized  List<Books>   selectMyShelfBook(String uid,String  bookCoverUrl,Context context,String  strLimit) {

		final  List<Books>   bookList = new ArrayList<>();
		try {
			MybookShelfEntityDao  mybookShelfDao = new MybookShelfEntityDao();
			List<MyBookShelf>  bookShelfs = mybookShelfDao.queryEntities("where  U_ID =?  order by  LAST_MODIFY_DATE  desc "+strLimit,uid);

			if(null!=bookShelfs  && bookShelfs.size()>0){
				for(int i=0;i<bookShelfs.size();i++){
					final MyBookShelf  mybook =bookShelfs.get(i);
					final  Books  book = selectBookById(mybook.getBookId(),bookCoverUrl,context);
					if(null!=book){
						book.setSaleFlag(ConstValues.MY_BOOK_SALES);
						book.setBsId(mybook.getBsId());
						bookList.add(book);
					}
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return bookList;
	}

	*//**
	 * 根据用户id查询对应书信息
	 * @param bookId
	 * @return
	 *//*
	private Books  selectBookById(final Integer bookId,final String  bookCover,final Context context) {
		BookEntityDao  bookDao=new BookEntityDao();
		final Books book = bookDao.queryEntityByWhere("where BOOK_ID = ? ",String.valueOf(bookId));
		  if(null!=book){
			  try{
				  JSONArray  partArray = new JSONArray(book.getPartStr());
				  book.setParts(partArray);
				  book.setCoverUrl(bookCover+book.getCoverUrl());
				  book.setSer(setBookCoverZipSer(partArray,context,book.getOriginalId()));
			  }catch (JSONException e){
				  e.printStackTrace();
			  }
		  }
		return   book;
	}

	private String[]   setBookCoverZipSer(final  JSONArray  jsonArray,final Context  context,final  String  original){
		String[]  serArray = null;
		try{
			String  savePath=utils.getBookZipsDownloadedFolder(utils.checkBooksDownloadedFolder(context),original,true);
			if(null!=jsonArray){
				serArray = new String[jsonArray.length()];
				for(int i=0;i<jsonArray.length();i++){
					String  ar =  jsonArray.getString(i);
					ar =  ar.substring(ar.lastIndexOf("/"),ar.length()).replace("zip","ser");
					String  saveP= savePath +ar;
					//  MyLog.e("查询返回返回的书架 书的图片......ser路径........." + saveP);
					serArray [i] = saveP;
				}
			}
		}catch(Exception  e){
			e.printStackTrace();
		}
		return  serArray;
	}

	*//**
	 *  book shelf  more
	 * @param context
	 * @param shelf
	 * @return
	 *//*
	public List<Books>    selectShelfBybsId(Context context,BookRack  shelf){
		final  List<Books>   bookList = new ArrayList<>();
		try{
			for(int i=0;i<shelf.getBsSaleFlag().length();i++){
				final  JSONObject object=(JSONObject)shelf.getBsSaleFlag().get(i);
				final int bookId = object.optInt("bookId");
				final int saleFlag = object.optInt("type");

				String   cover =SharedPreferenceUtils.getConfigUrlByKey(ApiConstans.CONFIG_BOOK_COVER,context);
				final  Books  books= selectBookById(bookId,cover,context);
				books.setSaleFlag(saleFlag);
				if(null!=books){
					bookList.add(books);
				}
			}
		}catch (JSONException e){
			e.printStackTrace();
		}
		return  bookList;
	}*/
	public List<Books> selectBooks(){
		BookEntityDao  bookDao=new BookEntityDao();
		List<Books> book = bookDao.queryEntities("");
         return book;
	}

}
